angular.module("dashboardApp")
.controller('menuCtrl', function($scope) {
	setTimeout(function() {
		$scope.$apply(function() {
			$scope.$parent.loadingView = false;
		})
	},1000);
})

// =========================================================================================
// =========================================================================================
// =========================================================================================

angular.module("dashboardApp")
.controller('commentCtrl', function($scope, $interval, cmtProcess) {
	$scope.pageView = 'check';
	$scope.checkList;

	cmtProcess.getData().then(function(data) {
		$scope.checkList = data;
		setTimeout(function() {
			$scope.$apply(function() {
				$scope.$parent.loadingView = false;
			})
		},1000);
	})
	
	// =============================================================
	$scope.checkView = function() {
		$scope.pageView = 'check';
	}
	$scope.reportView = function() {
		$scope.pageView = 'report';
	}

	$scope.checkAct = function(index) {
		id = $scope.checkList[index]._id['$id'];
		cmtProcess.checkCmt(id).then(function(msg) {
			if(msg == 'success') {
				getAlert('Đã check');
				$scope.checkList.splice(index, 1);
			}
			else getAlert('Xảy ra lỗi');
		})
	}
	$scope.delCheckAct = function(index) {
		id = $scope.checkList[index]._id['$id'];
		cmtProcess.deleteCmt(id).then(function(msg) {
			if(msg == 'success') {
				getAlert('Xóa thành công');
				$scope.checkList.splice(index, 1);
			}
			else getAlert('Xóa thất bại');
		})		
	}

	// =============================================================
	$interval(function() {
		cmtProcess.getData().then(function(data) {
			$scope.checkList = data;
		});
	}, 120000); // 2 minutes

	$('#tabs .nav a').click(function() {
		$(this).tab('show');
	})
})
.factory('cmtProcess', function($http, BASE_URL) {
	return {
		getData: function() {
			return $http.post(BASE_URL + 'application_api/comment/rawComment')
			.then(function(promise) {
				return promise.data;
			})
		},
		checkCmt: function(id) {
			return $http.post(BASE_URL + 'application_api/comment/checkCmt/' + id)
			.then(function(promise) {
				return promise.data;
			})
		},
		deleteCmt: function(id) {
			return $http.post(BASE_URL + 'application_api/comment/deleteCmt/' + id)
			.then(function(promise) {
				return promise.data;
			})
		}
	}
})